package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.repository.UserInfoRepository;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.AssertUtil;
import cn.wolfcode.luowowo.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;


    @Override
    public UserInfo get(String id) {
        Optional<UserInfo> op = userInfoRepository.findById(id);
        return op.orElse(null);
    }

    //检查手机号码是否被注册
    @Override
    public boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoRepository.findByPhone(phone);
        return userInfo == null;
    }

    //发送短信验证码 ,并将验证码存到redis数据库中,注册时进行判断
    @Override
    public void sendVerifyCode(String phone, String smsAppkey, String smsUrl, String smsSign) {
        //随机生成4位数
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //模拟发送短信
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("你好,短信验证码是").append(code).append("请在").
                append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        System.out.println(stringBuilder);

        //java web方法发生
        RestTemplate template = new RestTemplate();
        //真实发短信解决硬编码问题
        String ret = template.getForObject(smsUrl,String.class,smsSign,phone,stringBuilder.toString(),smsAppkey);
        System.out.println(ret);
        //判断是否发生成功
        if(!ret.contains("SUCCESS")){
            throw  new LogicException("短信发送失败");
        }

        //缓存验证码
        userInfoRedisService.setVerifyCode(phone,code);
    }


    //完成用户注册功能
    @Override
    public void regist(String phone, String nickname, String password, String rpassword ,String verifyCode) {
        //1所有的参数判空 使用自定义工具类
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"密码不能为空");
        //2判断两次密码是否一样
        AssertUtil.isEquals(password,rpassword,"密码不一致,请重新输入");
        //3手机号码是否唯一
        if(! this.checkPhone(phone)){
            throw new LogicException("手机号码已经存在");
        }
        //判断手机格式
        //4判断验证码是否一致 ,获取redis中的key' 值
        //String key = RedisKeys.VERIFY_CODE.join(phone);
        String value = userInfoRedisService.getKey(phone);
        System.out.println(value);
        System.out.println(verifyCode);

        if(value == null ||!value.equals(verifyCode)){
            throw new LogicException("验证码错误");
        }
        //5注册将用户名信息保存到数据库中
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setEmail("");//先设置为""不设置的话,表中会没有这个字段
        userInfo.setCity("");
        userInfo.setInfo("");//签名
        userInfo.setLevel(1);//等级
        userInfo.setHeadImgUrl("/images/default.jpg");//头像
        userInfo.setState(UserInfo.STATE_NORMAL);//账号正常
        //保存到数据库
        userInfoRepository.save(userInfo);





    }

    //登入判断用户名和密码
    @Override
    public UserInfo checkLogin(String username, String password) {
        UserInfo userInfo = userInfoRepository.findByPhone(username);
        //判断用户名和密码
        if(userInfo == null ||! password.equals(userInfo.getPassword())){
            throw new LogicException("用户名或者密码错误");
        }
        return userInfo;
    }


}
